import request, { ApiResponse } from './request'

// 权限信息（根据 API 响应定义）
export interface Permission {
  id: string
  name: string
  code: string
  description: string
  created_at?: string
}

// 创建权限参数
export interface CreatePermissionParams {
  name: string
  code: string
  description?: string
}

// 更新权限参数
export interface UpdatePermissionParams {
  id: string
  name?: string
  code?: string
  description?: string
}

// 删除权限参数
export interface DeletePermissionParams {
  id: string
}

// 删除权限响应
export interface DeletePermissionResponse {
  message: string
}

// 权限列表查询参数
export interface PermissionListParams {
  page?: number
  pageSize?: number
  name?: string
  code?: string
}

// 权限列表响应
export interface PermissionListResponse {
  list: Permission[]
  total: number
  page: number
  pageSize: number
}

// 权限详情参数
export interface PermissionDetailParams {
  id: string
}

// 创建权限
export const createPermission = (data: CreatePermissionParams): Promise<ApiResponse<Permission>> => {
  return request.post<Permission>('/permissions/create', data)
}

// 更新权限
export const updatePermission = (data: UpdatePermissionParams): Promise<ApiResponse<Permission>> => {
  return request.post<Permission>('/permissions/update', data)
}

// 删除权限
export const deletePermission = (data: DeletePermissionParams): Promise<ApiResponse<DeletePermissionResponse>> => {
  return request.post<DeletePermissionResponse>('/permissions/delete', data)
}

// 获取权限列表
export const getPermissionList = (data?: PermissionListParams): Promise<ApiResponse<PermissionListResponse>> => {
  return request.post<PermissionListResponse>('/permissions/list', data || {})
}

// 获取权限详情
export const getPermissionDetail = (data: PermissionDetailParams): Promise<ApiResponse<Permission>> => {
  return request.post<Permission>('/permissions/detail', data)
}

// 获取权限列表（用于下拉选择，获取全部权限）
export const getAllPermissionList = (data?: PermissionListParams): Promise<ApiResponse<PermissionListResponse>> => {
  // 获取所有权限，设置较大的 pageSize
  return request.post<PermissionListResponse>('/permissions/list', {
    page: 1,
    pageSize: 1000,
    ...data
  })
}

